﻿using Cloudthentic.SSIS.Server;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.SqlServer.Dts.Runtime;
using System.Collections.Generic;
using Cloudthentic.SSIS.Server.Configuration;

namespace Cloudthentic.SSIS.Server.Tests
{
    
    
    /// <summary>
    ///This is a test class for ExecutorTest and is intended
    ///to contain all ExecutorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ExecutorTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Executor Constructor
        ///</summary>
        [TestMethod()]
        public void ExecutorConstructorTest()
        {
            Executor target = new Executor();
        }


        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void ExecuteTest()
        {
            SsisApplication target = new SsisApplication();
            string packageName = "SsisPackage1";
            List<SsisVariable> variables = new List<SsisVariable>();
            target.ExecutePackage(packageName, variables);
        }

        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void ExecuteTest2()
        {

            
            Action<object> action = (object obj) =>
            {
                SsisApplication target = new SsisApplication();
                string packageName = "SsisPackage1";

                List<SsisVariable> variables = new List<SsisVariable>();
                SsisVariable variable = new SsisVariable();
                variable.Name = "Sleep";
                variable.TypeCode = TypeCode.Int32;
                variable.Value = 10000;
                variables.Add(variable);

                target.ExecutePackage(packageName, variables);

            };
            System.Threading.Tasks.Task task1 = new System.Threading.Tasks.Task(action, "task1");

            
            Action<object> action2 = (object obj) =>
            {

                SsisApplication target2 = new SsisApplication();
                string packageName = "SsisPackage1";

                List<SsisVariable> variables = new List<SsisVariable>();
                SsisVariable variable = new SsisVariable();
                variable.Name = "Sleep";
                variable.TypeCode = TypeCode.Int32;
                variable.Value = 1000;
                variables.Add(variable);

                target2.ExecutePackage(packageName, variables);

            };
            System.Threading.Tasks.Task task2 = new System.Threading.Tasks.Task(action2, "task2");

            task1.Start();

            Microsoft.SqlServer.Dts.Runtime.Application app = new Application();
            int count = 0; int iteration = 0;
            while(!(iteration > 10 || count == 1))
            {
                System.Threading.Thread.Sleep(50);
                Microsoft.SqlServer.Dts.Runtime.RunningPackages packages = app.GetRunningPackages("localhost");
                count = packages.Count;
            }

            Assert.IsTrue(count == 1);

            task2.Start();

            iteration = 0;
            while (!(iteration > 10|| count == 2))
            {
                System.Threading.Thread.Sleep(50);
                Microsoft.SqlServer.Dts.Runtime.RunningPackages packages = app.GetRunningPackages("localhost");
                count = packages.Count;
            }

            Assert.IsTrue(count == 2);


        }



        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void ExecuteTest1()
        {
            IExecutor target = new Executor(); // TODO: Initialize to an appropriate value
            string packageName = string.Empty; // TODO: Initialize to an appropriate value
            List<SsisVariable> variables = null; // TODO: Initialize to an appropriate value
            target.Execute(packageName, variables);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for Executor Constructor
        ///</summary>
        [TestMethod()]
        public void ExecutorConstructorTest1()
        {
            Executor target = new Executor();
            Assert.Inconclusive("TODO: Implement code to verify target");
        }

        /// <summary>
        ///A test for AssignVariables
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Cloudthentic.SSIS.Server.dll")]
        public void AssignVariablesTest()
        {
            Executor_Accessor target = new Executor_Accessor(); // TODO: Initialize to an appropriate value
            Package pkg = null; // TODO: Initialize to an appropriate value
            List<SsisVariable> variables = null; // TODO: Initialize to an appropriate value
            target.AssignVariables(pkg, variables);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void ExecuteTest3()
        {
            Executor target = new Executor(); // TODO: Initialize to an appropriate value
            string packageName = string.Empty; // TODO: Initialize to an appropriate value
            List<SsisVariable> variables = null; // TODO: Initialize to an appropriate value
            target.Execute(packageName, variables);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for ResolvePackagePath
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Cloudthentic.SSIS.Server.dll")]
        public void ResolvePackagePathTest()
        {
            Executor_Accessor target = new Executor_Accessor(); // TODO: Initialize to an appropriate value
            string packageName = "SsisPackage1";
            string expected = @"I:\Work\Acutulus Software\SSISAppServer\Source\Cloudthentic.SSIS.Server.Tests\Resources\Package1.dtsx";

            SsisApplication app = new SsisApplication();
            target.SsisServerConfiguration = app.GetReadOnlyConfiguration();
            string actual;
            actual = target.ResolvePackagePath(packageName);
            Assert.AreEqual(expected, actual);

        }

        /// <summary>
        ///A test for SsisServerConfiguration
        ///</summary>
        [TestMethod()]
        public void SsisServerConfigurationTest()
        {
            Executor target = new Executor(); // TODO: Initialize to an appropriate value
            SsisServerConfiguration expected = null; // TODO: Initialize to an appropriate value
            SsisServerConfiguration actual;
            target.SsisServerConfiguration = expected;
            actual = target.SsisServerConfiguration;
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
